package com.yanqu.road.dao.impl.recharge;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.recharge.RechargeMallTimes;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RechargeMallTimesDaoImpl extends TempDataObjectDao<RechargeMallTimes> {


    public static String getRechargeMallTimesSql = "select * from `t_u_recharge_mall_times` where `userId`=?";
    public Map<Integer, Map<Integer, RechargeMallTimes>> getRechargeMallTimes(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, RechargeMallTimes>> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getRechargeMallTimesSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                RechargeMallTimes data =  parse(rs);
                if(!resultMap.containsKey(data.getMallId())){
                    resultMap.put(data.getMallId(), new ConcurrentHashMap<>());
                }
                resultMap.get(data.getMallId()).put(data.getSrc(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String insertRechargeMallTimesSql = "insert into `t_u_recharge_mall_times`(userId,mallId,src,times) values (?,?,?,?)";

    @Override
    protected boolean addData(RechargeMallTimes data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSrc()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimes()));
        if (getDb().executeNoneQuery(insertRechargeMallTimesSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateRechargeMallTimesSql = "update `t_u_recharge_mall_times` set times=? where userId=? and mallId=? and src=?";

    @Override
    protected boolean updateData(RechargeMallTimes data) {
        updateRechargeMallTimesSql = "update `t_u_recharge_mall_times` set times=? where userId=? and mallId=? and src=?";
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSrc()));
        if (getDb().executeNoneQuery(updateRechargeMallTimesSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected RechargeMallTimes parseData(ResultSet rs) throws SQLException {
        RechargeMallTimes data = new RechargeMallTimes();
        data.setUserId(rs.getLong("userId"));
        data.setMallId(rs.getInt("mallId"));
        data.setSrc(rs.getInt("src"));
        data.setTimes(rs.getInt("times"));

        return data;
    }

    /**
     * 重置首充状态
     */
    public boolean resetFirstRecharge() {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_recharge_mall_times SET times = 0";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            call.executeUpdate();
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return true;
    }

    /**
     * 重置首充状态
     */
    public boolean resetRechargeTimes(int src) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_recharge_mall_times SET times = 0 where src = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            call.setInt(1, src);
            call.executeUpdate();
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return true;
    }
}
